﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KAKA.Domain;
using Morus;
using Morus.Infrastructure;

namespace KAKA.Repository
{
    public interface IMemberRepository : IRepository<Member>
    {
        Member Get(Guid id);

        IEnumerable<Member> GetAll();

        IEnumerable<Member> Find(ISpecification<Member> specification);

        IEnumerable<Member> GetNewlyGirls(DateTime timestamp, int size);

        IEnumerable<Member> GetNewlyBoys(DateTime timestamp, int size);
    }

    public class MemberRepository : Repository<Member>, IMemberRepository
    {
        public MemberRepository(Database database)
            : base(database)
        {
        }

        public Member Get(Guid id)
        {
            return base.Get(Spec<Member>.Eval(r => r.Id == id));
        }

        public new IEnumerable<Member> Find(ISpecification<Member> specification)
        {
            return base.Find(specification).ToArray();
        }

        public new IEnumerable<Member> GetAll()
        {
            return base.GetAll().ToArray();
        }

        public IEnumerable<Member> GetNewlyGirls(DateTime timestamp, int size)
        {
            return base.GetAll().Where(r => r.Gender == Gender.Female && r.Portrait != null && r.User.CreateAt >= timestamp).Take(size).ToArray();
        }

        public IEnumerable<Member> GetNewlyBoys(DateTime timestamp, int size)
        {
            return base.GetAll().Where(r => r.Gender == Gender.Male && r.Portrait != null && r.User.CreateAt >= timestamp).Take(size).ToArray();
        }
    }
}
